package Example;

public class Solution {
    public static void main(String[] args) {
        String str="123";
        System.out.println(StrToInt(str));

    }
    public static int StrToInt(String str) {
        int sign=1;
        char[] chars=str.trim().toCharArray();
        if(chars.length==0){
            return 0;
        }
        int i=1;
        if(chars[0]=='-'){
            sign=-1;
        }else if(chars[0]!='+'){
            i--;
        }
        int res=0;
        int temp=0;
        for(;i<chars.length;i++){
            if(!Character.isDigit(chars[i])){
                return 0;
            }
            res=10*res+(chars[i]-'0');
            if(res/10!=temp){
                return 0;
            }
            temp=res;
        }
        res*=sign;
        return res;
    }
}